package com.jackeysun.app_widget

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.jackeysun.app_widget.databinding.ActivityMain5Binding
import com.jackeysun.widget.tree.model.TreeNode
import com.jackeysun.widget.tree.view.AndroidTreeView

class MainActivity5 : AppCompatActivity(), TreeNode.TreeNodeClickListener {

    private lateinit var mBinding : ActivityMain5Binding

    private lateinit var tView:AndroidTreeView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMain5Binding.inflate(layoutInflater)
        setContentView(mBinding.root)

//        val root: TreeNode = TreeNode.root()
//
//        val s1: TreeNode =
//            TreeNode(IconTreeItemHolder.IconTreeItem(0,
//                "Folder with very long name " +
//                        "Folder with very long name Folder with very long name Folder with very long name ")).setViewHolder(
//                IconTreeItemHolder(this))
//        val s2: TreeNode = TreeNode(IconTreeItemHolder.IconTreeItem(1,
//            "Another folder with very long name")).setViewHolder(
//            IconTreeItemHolder(this))
//
//        fillFolder(s1)
//        fillFolder(s2)
//
//        root.addChildren(s1)
//        root.addChildren(s2)

//        tView = AndroidTreeView(this, root)
//        tView.setDefaultAnimation(true)
////        tView.setUse2dScroll(true)
//        tView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom)
//        tView.setDefaultNodeClickListener(this@MainActivity5)
//        tView.setDefaultViewHolder(IconTreeItemHolder::class.java)
//        mBinding.container.removeAllViews()
//        mBinding.container.addView(tView.view)
//        tView.setUseAutoToggle(false)
////        tView.expandAll()


        val root = TreeNode.root()

        val s1 =
            TreeNode(IconTreeItemHolder.IconTreeItem(R.string.ic_folder,
                "Folder with very long name Folder with very long name Folder with very long name Folder with very long name Folder with very long name Folder with very long name ")).setViewHolder(
                ArrowExpandSelectableHeaderHolder(this))
        val s2 = TreeNode(IconTreeItemHolder.IconTreeItem(R.string.ic_folder,
            "Another folder with very long name")).setViewHolder(
            ArrowExpandSelectableHeaderHolder(this))

        fillFolder(s1)
        fillFolder(s2)

        root.addChildren(s1, s2)

        tView = AndroidTreeView(this, root)
        tView.setDefaultAnimation(true)
//        tView.setUse2dScroll(true)
        tView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom)
        tView.setDefaultNodeClickListener(this)
        tView.setDefaultViewHolder(ArrowExpandSelectableHeaderHolder::class.java)
        mBinding.container.addView(tView.view)
//        tView.setUseAutoToggle(false)
        tView.expandAll()
        if (savedInstanceState != null) {
            val state = savedInstanceState.getString("tState")
            if (!TextUtils.isEmpty(state)) {
                tView.restoreState(state)
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString("tState", tView.saveState)
    }

    override fun onClick(node: TreeNode?, value: Any?) {
        Toast.makeText(this, (value as IconTreeItemHolder.IconTreeItem).text, Toast.LENGTH_LONG).show()
    }

    private fun fillFolder(folder: TreeNode) {
        var currentNode = folder
        for (i in 0..3) {
            val file = TreeNode(IconTreeItemHolder.IconTreeItem(R.string.ic_folder,
                  "Very long name for folder $i"))
            currentNode.addChild(file)
            currentNode = file
        }
    }
}